package cn.iocoder.yudao.module.vlog.product.controller.app;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.api.product.spu.ProductSpuApi;
import cn.iocoder.yudao.module.api.product.spu.dto.ProductSpuRespDTO;
import cn.iocoder.yudao.module.vlog.info.dal.dataobject.VLogInfoDO;
import cn.iocoder.yudao.module.vlog.info.service.VLogInfoService;
import cn.iocoder.yudao.module.vlog.product.service.VLogProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "用户 App - 视频带货")
@RestController
@RequestMapping("/vlog/product")
@Validated
public class AppVLogProductController {

    @Resource
    private VLogInfoService vLogInfoService;
    @Resource
    private VLogProductService vLogProductService;
    @Resource
    private ProductSpuApi productSpuApi;

    @GetMapping("/list")
    @Operation(summary = "获得视频带货列表")
    @Parameter(name = "vlogId", description = "视频编号", required = true, example = "1024")
    @Parameter(name = "limit", description = "限制条数", example = "100")
    @PermitAll
    public CommonResult<List<ProductSpuRespDTO>> getProductByVlogId(@RequestParam("vlogId") Long vlogId,
                                                                    @RequestParam(name = "limit", required = false) Integer limit) {
        VLogInfoDO vlogInfoDO = vLogInfoService.getInfo(vlogId);
        Set<Long> spuIds = vLogProductService.getProductSpuIds(vlogInfoDO.getUserId(), vlogInfoDO.getShopId(), vlogId, limit);
        List<ProductSpuRespDTO> spus = productSpuApi.getSpuList(spuIds);
        return success(spus);
    }

}
